<%-- 
    Document   : file-upload
    Created on : 6/Abr/2010, 21:28:32
    Author     : luis
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"
   import="java.io.*"
   import="java.util.*"
   import="org.apache.commons.fileupload.*"
   import="org.apache.commons.io.*"
   import="org.json.*"

 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%
 
    boolean isMultipart = FileUpload.isMultipartContent(request);

    if (isMultipart) {
        
      // Create a new file upload handler
      DiskFileUpload upload = new DiskFileUpload();
        
      // Set upload parameters
      upload.setSizeMax(50*1024*1024); //50Mb
      upload.setRepositoryPath("/tmp");

      // Parse the request
     List items = upload.parseRequest(request);

      Iterator iter = items.iterator();
      while (iter.hasNext()) {
          
         FileItem fitem = (FileItem) iter.next();
         if (!fitem.isFormField()) {
             JSONObject obj = new JSONObject();
             obj.put("success", true);
             obj.put("file", fitem.getString());
             out.print(obj);
         }
      
   }
}
    

%>